#include "StdAfx.h"
//-------------------------------------------------------------------------------------------------
//DrawingTools.cpp : Class for Creating Tools used to draw Image Properties (its ImageObjects)    
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk July 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <string>

#include "resource.h"

#include "ImageRect.h"
#include "DrawingTools.h"
#include "PtShapeTool.h"
#include "PtAnnotateShapeTool.h"
#include "EllipseTool.h"
#include "SelectTool.h"


DrawingTools::DrawingTools(void) 
{
}


DrawingTools::~DrawingTools(void)
{
	ClearAllTools();
}


bool DrawingTools::CreateImageObjectTypeTools (ImageDataset *pImageDataset)
{
	bool rc = true;

	ClearAllTools();

	if ( pImageDataset != '\0') 
	{
		ImageObjectType *pObjType = pImageDataset->StartImageObjectTypeIter();
		while ((pObjType = pImageDataset->GetNextImageObjectType()) != '\0')
		{
			ImageObjectTypeTool *pTool = GetToolForImageObjectType(pObjType);
			if (pTool == '\0')
			{
				if (strcmp (pObjType->GetBaseClassName(), SelectType::baseClassName) == 0 )
				{
					if ( AddTool( new SelectTool(pObjType) ) == false)
					{
						rc = false;
						break;
					}
				}
				if (strcmp (pObjType->GetBaseClassName(), PtShapeType::baseClassName) == 0 )
				{
					if ( AddTool( new PtShapeTool(pObjType) ) == false)
					{
						rc = false;
						break;
					}
				}
				if (strcmp (pObjType->GetBaseClassName(), EllipseShapeType::baseClassName) == 0 )
				{
					if ( AddTool( new EllipseTool(pObjType) ) == false)
					{
						rc = false;
						break;
					}
				}
				if (strcmp (pObjType->GetBaseClassName(), PtAnnotateShapeType::baseClassName) == 0 )
				{
					if ( AddTool( new PtAnnotateShapeTool(pObjType) ) == false)
					{
						rc = false;
						break;
					}
				}
			}
		}
	}
	return rc;
}

ImageObjectTypeTool *DrawingTools::GetToolForImageObject (ImageObject *pImageObject)
{
	ImageObjectTypeTool *rc = '\0';

	if ( pImageObject )
		rc = GetToolForImageObjectType(pImageObject->GetImageObjectType());

	return rc;
}


ImageObjectTypeTool *DrawingTools::GetToolForImageObjectType (ImageObjectType *pImageObjectType)
{
	ImageObjectTypeTool *rc = '\0';

	if (pImageObjectType)
	{
		size_t cnt = _Tools.size();
		if (cnt > 0 )
		{
			ImageObjectType *pListObjectType = '\0';
			for (size_t x = 0; x < cnt; x++)
			{
				if ( strcmp(_Tools[x]->GetImageObjectType()->GetName(), pImageObjectType->GetName()) == 0)
				{
					rc = _Tools[x];
					break;
				}
			}
		}
	}
	return rc;
}

ImageObjectTypeTool *DrawingTools::GetToolForImageObjectTypeName (const char *ImageObjectTypeName)
{
	ImageObjectTypeTool *rc = '\0';

	if (ImageObjectTypeName)
	{
		std::string Name(ImageObjectTypeName);
		size_t cnt = _Tools.size();
		if (cnt > 0 )
		{
			ImageObjectType *pListObjectType = '\0';
			for (size_t x = 0; x < cnt; x++)
			{
				if ( Name == _Tools[x]->GetImageObjectType()->GetName())
				{
					rc = _Tools[x];
					break;
				}
			}
		}
	}
	return rc;
}

bool DrawingTools::SetupDynamicMenus(CMFCPopupMenu* pMenuPopup, int iIndex, ImageType *pSelImageType, ImageDataset* pIdfDoc)
{
	bool rc = false;

	if ( (pMenuPopup != '\0') && (pIdfDoc != '\0') )
	{
		int menuIndex = iIndex;

		ImageObjectTypeTool *pTool = '\0';

		ImageObjectType *pObjType = pIdfDoc->StartImageObjectTypeIter();
		while ((pObjType = pIdfDoc->GetNextImageObjectType()) != '\0')
		{
			if ( pSelImageType->GetImageObjectTypeForMenuId(pObjType->GetResourceId()) != '\0' )
			{
				if (pTool = GetToolForImageObjectType(pObjType))
				{
					if (pTool->InsertDynMenuItem(pMenuPopup, menuIndex) == -1)
						break;
					menuIndex++;
					if ( pTool->GetResourceId() == IDS_SELECTTYPE )
					{
						pMenuPopup->InsertSeparator (menuIndex);
						menuIndex++;
					}
				}
			}
		}
		if ((menuIndex != -1) && (pTool != '\0'))
			rc = true;
	}
	return rc;
}

bool DrawingTools::DrawImageDynamicMenu(CDC* pDC, const CMFCToolBarMenuButton* pMenuButton, const CRect& rectImage, ImageDataset* pIdfDoc, bool select)
{
	bool rc = false;

	if ( (pIdfDoc != '\0' ) && (pDC != '\0' ) && (pMenuButton != '\0' ) )
	{
		ImageRect	menuIconRect(rectImage.TopLeft().x, rectImage.TopLeft().y, rectImage.BottomRight().x, rectImage.BottomRight().y);
		ImageObjectTypeTool *pTool = '\0';
		if (pTool = GetToolForImageObjectType(pIdfDoc->GetImageObjectTypeForMenuId(pMenuButton->m_nID)))
			 rc = pTool->DrawImageDynMenu(pDC, pMenuButton, menuIconRect, select); 

	}
	return rc;
}
//............................................................................................
//private member functions
//............................................................................................

bool DrawingTools::ClearAllTools()
{
	bool rc = false;

	size_t cnt = _Tools.size();
	if (cnt > 0 )
	{
		for (size_t x = 0; x < cnt; x++)
			delete _Tools[x];
		_Tools.clear();
		rc = true;
	}
	return rc;
}

bool DrawingTools::AddTool( ImageObjectTypeTool *pTool )
{
	bool rc = false;

	_Tools.push_back(pTool);

	rc = true;

	return rc;
}

//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------
